<?php

namespace Rblog\Module\Admin\Controller;

class Comment extends Index {
    
    private $_model;
    
    public function __construct() {
        $this->_working = 'comment';
        parent::__construct();
        $this->_model = \Rexzy::model( 'Comment' ); 
    }
    
    /**
     * 评论列表
     */
    public function show() {
        $length = 10;
        $condition = array();
        
        // 获取列表信息
        if ( $this->request->input('page', 'g' ) != false ) {
            $page = $this->request->input('page', 'g' );
        } else {
            $page = 1;
        }
        
        $start = ( $page - 1 ) * $length;
        if ( $this->request->isPost() ) {
            $condition['search'] = $this->request->input('search');
        } else if ( $this->request->input('search', 'g' ) != false ) {
            $condition['search'] = $this->request->input('search', 'g' );
        }
        $total = count( $this->_model->getList( $condition ) );
        $commentList = $this->_model->getList( $condition, $start, $length );
        
        $keyword = '任何';
        if ( count( $condition ) != 0 ) {
            $returnHome = true;
            $keyword = "包含关键字 '" . $condition['search']. "' 的";
        } else {
            $returnHome = false;
        }
        
        // 整理分页信息
        $args = array(
            'rang' => 5,
            'length' => $length,
            'total' => $total,
            'page' => $page,
            'url' => \Rexzy::url('admin/comment'),
            'condition' => $condition,
            'comments' => $commentList,
            'authorurl' => \Rexzy::url('author/'),
            'arturl' => \Rexzy::url('art/'),
            'keyword' => $keyword,
            'returnHome' => $returnHome
            );
        
        $this->view->assign( $args );
        $this->view->display();
    }
    
    /**
     * 删除评论
     */
    public function delcomment() {
        $post = $this->request->input( 'value', 'p' );
        
        $commentids = explode( ',', $post );
        $result = $this->_model->del( $commentids );
        
        if ( $result > 0 ) {
            echo json_encode( array( 'delresult' => true ) );
        } else {
            echo json_encode( array( 'delresult' => false ) );
        }
    }
}
